package cn.usian.controller;

import cn.usian.pojo.Order;
import cn.usian.pojo.User;
import cn.usian.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private RestTemplate restTemplate;
    @GetMapping("{id}")
    public Order  findById(@PathVariable long id){
        Order order = orderService.findById(id);
        //创建远程服务的地址
        //String url="http://localhost:8082/user/"+order.getUserId();
        String url="http://userservice/user/"+order.getUserId();
        //远程调用
        User user = restTemplate.getForObject(url, User.class);
        //将user对象保存在order对象中
        order.setUser(user);
        return order;
    }


    @GetMapping("findPage")
    public String findPage(@RequestParam(required = true,defaultValue = "1")Integer pageIndex,String name){

        String page = restTemplate.getForObject("http://userservice/user/findPage?pageIndex="+pageIndex+"&name="+name,String.class);
        return page;
    }

    @GetMapping("del")
    public String del(Integer id){
        String forObject = restTemplate.getForObject("http://userservice/user/del?id=" + id, String.class);

        return  forObject;
    }

    @PostMapping("add")
    public String add(@RequestBody User user){
        String forObject = restTemplate.postForObject("http://userservice/user/add",user,String.class);
        return forObject;
    }

    @GetMapping("findById")
    public String findById(Integer id){
        String forObject = restTemplate.getForObject("http://userservice/user/findById?id=" + id, String.class);
        return forObject;
    }

    @PostMapping("edit")
    public String edit(@RequestBody User user){
        String forObject = restTemplate.postForObject("http://userservice/user/edit",user,String.class);
        return forObject;
    }
}
